package hdf5.gui;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.List;


public class Player implements Runnable {

    private static volatile Player instance;

    private static MapDrawer mapDrawer;
    private int currentStep = 0;
    private static List<double[][]> array2DList;

    private Player() {
    }

    public static void setMapDrawer(MapDrawer mapDrawer) {
        Player.mapDrawer = mapDrawer;
    }

    public static void setArray2DList(List<double[][]> array2DList) {
        Player.array2DList = array2DList;
    }

    public static synchronized Player getInstance() {
        if (instance == null) {
            instance = new Player();
        }
        return instance;
    }


    public void run() {
        while (this.next()) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                return;
            }
        }
    }

    public boolean next() {
        if (currentStep < array2DList.size() - 1) {
            currentStep++;
            setStep(currentStep);
            return true;
        } else {
            return false;
        }

    }

    public boolean previous() {
        if (currentStep > 0) {
            currentStep--;
            setStep(currentStep);
            return true;
        } else {
            return false;
        }
    }

    public void setStep(int time) {
        mapDrawer.setArray2D(array2DList.get(time));
    }

    public int getStep() {
        return currentStep;
    }

    public double[][] getArray2D() {
        return array2DList.get(currentStep);
    }

    public BufferedImage getImage() {
        final int width = 800;
        final int height = 800;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        g2d.drawImage(mapDrawer.getImage(), 0, 0, width, height, null);
        return image;
    }
}
